import os
import random
import time
import math
import requests
import json
import jsonpath
import argparse

DOWNLOAD_DIR = None

UA_li = [
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; rv:11.0) like Gecko',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
    'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
]


def download_img(num, **kw):
    if num < 0:
        return 1
    if num > 5000:
        num = 5000

    url_prefix = 'https://img2.huashi6.com/'
    pic_data_url = 'https://rt.huashi6.com/front/index/load_pc_data'

    cnt = 0
    cur_batch = 2  # batch最大为151
    pass_num = 0  # 跳过的图片数
    extensions = kw['extensions']
    post_times = 1 if num <= 20 else math.floor(num/20)+1  # 计算下载num张图片至少需要请求多少次
    for i in range(post_times):
        print("---------------------------------------------------------")
        print(f"下载第{i + 1}组图片中..., cur_batch: {cur_batch}")
        headers = {
            'User-Agent': random.choice(UA_li)
        }
        # ts参数为毫秒时间戳(将当前时间的时间戳与浏览器中发送请求的参数中的ts值进行对比可知)
        time_ms = time.time() * 1000
        _ts_ = str(time_ms)
        # 观察该接口在浏览器中请求传递的cursor参数，如：29-1671251923043，前面是第几个batch,后面是毫秒时间戳
        # 浏览器在请求该接口时，第一次请求时，前面的数字(batch)从2开始的
        cursor = str(cur_batch) + '-' + str(time_ms)
        data = {
            '_ts_': _ts_,
            'cursor': cursor
        }
        response = requests.get(url=pic_data_url, data=data, headers=headers)
        content = response.text
        pic_data_json = json.loads(content)
        # print(pic_data_json)
        code = jsonpath.jsonpath(pic_data_json, '$.code')[0]
        if code != 0:
            print("获取数据失败")
            return -1
        count = jsonpath.jsonpath(pic_data_json, '$.data.count')[0]
        paths = jsonpath.jsonpath(pic_data_json, '$..coverImage.originalPath')
        hasNext = jsonpath.jsonpath(pic_data_json, '$.data.hasNext')[0]
        if hasNext is not True:
            print("已没有更多内容\n")
            return 0
        cursor = jsonpath.jsonpath(pic_data_json, '$.data.nextCursor')[0]
        for path in paths:
            file_extension = path.split('.')[-1]
            img_name = path.split('.')[0].split('thumbnail/')[-1]
            img_name = img_name.replace('/', '_')

            pass_flag = True
            for extension in extensions:
                if file_extension == extension:  # 扩展名符合要求
                    pass_flag = False
                    break
            if pass_flag is True:
                print(f"跳过一张扩展名不符合要求的图片...\n")
                pass_num += 1
                if post_times * 20 - num - pass_num < 0:
                    post_times += 1
                continue

            # date = time.strftime("%Y_%m_%d", time.localtime())
            img_url = url_prefix + path
            img = requests.get(img_url).content
            print(f"第{cnt + 1}张图片下载中...")
            with open(DOWNLOAD_DIR + img_name + '.' + file_extension, 'wb') as fp:  # 图像要以二进制的形式写入
                fp.write(img)
            cnt += 1
            print(f"第{cnt}张图片下载完毕.\n")
            time.sleep(1)
            if cnt == num:
                return 1
        cur_batch += 1
    return -2


if __name__ == "__main__":
    parse = argparse.ArgumentParser()
    parse.add_argument("--num", type=int, required=False, default=2000, help='download num')
    parse.add_argument("--dir", type=str, required=False, default='imgs/', help='download dir')

    args = parse.parse_args()
    NUM = args.num
    DOWNLOAD_DIR = args.dir

    start_time = time.time()

    if not os.path.isdir(DOWNLOAD_DIR):
        os.mkdir(DOWNLOAD_DIR)

    status = download_img(NUM, extensions=['jpg'])
    time_cost = time.time() - start_time
    print(f"共耗时{int(time_cost)}秒，{int(time_cost/60)}分钟")
